<?php

/**
 * planning actions.
 *
 * @package    vo-ser
 * @subpackage planning
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class planningActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {      
    $this->redirect('@planning_calendar?pr_slug='.$r->getParameter('pr_slug'));
  
  }
  
  public function executeCalendarNoDates(sfWebRequest $r)
  {
          // Get today's date.
      $date = time ();
      $month = date ( 'm', $date );
      $year = date ( 'Y', $date );
      
      $this->redirect('@planning_calendar?pr_slug='.$r->getParameter('pr_slug').'&month='.$month.'&year='.$year/*.'&dueBefore=0&importance=0&group=0&createdBy=0'*/);
  }
  
  public function executeCalendar(sfWebRequest $r)
  {
    // Get needed info from request.
    $this->prSlug = $r->getParameter('pr_slug');
    $this->projName = SER::deSlugify($this->prSlug);
    $this->deadList = array();
    $this->deadList = $this->getDeadList($this->projName);
    //$this->deadList = $this->sortDeadList
    $this->year=$r->getParameter('year');
    $this->month=$r->getParameter('month');
    
    // Get list of search criteria from url if possible, else set the missing values to 0.
    if (isset($_GET['dueBefore']))
        {$this->dueBefore=$_GET['dueBefore'];}
    else
        {$this->dueBefore="0";}
    if (isset($_GET['importance']))
        {$this->importance=$_GET['importance'];}
    else
        {$this->importance="0";}
    if (isset($_GET['group']))
        {$this->group=$_GET['group'];}
    else
        {$this->group="0";}
    if (isset($_GET['createdBy']))
        { $this->createdBy=$_GET['createdBy'];}
    else
        {$this->createdBy="0";}
    
    // Calculate next and previous years.
    $this->prev_year = $this->year;
    $this->prev_month = $this->month - 1;
    if ($this->prev_month < 1) {
        $this->prev_month = 12;
        $this->prev_year = $this->year - 1;
    }
    $this->next_year = $this->year;
    $this->next_month = $this->month + 1;
    if ($this->next_month > 12) {
        $this->next_month = 1;
        $this->next_year = $this->year + 1;
    }
    // Generate the first day of the month.
    $this->first_day = mktime ( 0, 0, 0, $this->month, 1, $this->year );
    // Get month title.
    $this->title = date ( 'F', $this->first_day );
    // Find first day of month.
    $this->day_of_week = date ( 'D', $this->first_day );
    // Find how many blank days before first day of week.
    switch ($this->day_of_week) {
        case "Sun" :
            $this->blank = 0;
            break;
        case "Mon" :
            $this->blank = 1;
            break;
        case "Tue" :
            $this->blank = 2;
            break;
        case "Wed" :
            $this->blank = 3;
            break;
        case "Thu" :
            $this->blank = 4;
            break;
        case "Fri" :
            $this->blank = 5;
            break;
        case "Sat" :
            $this->blank = 6;
            break;
    }
    // Calculate days in current month.
    $this->days_in_month = cal_days_in_month ( 0, $this->month, $this->year );
  }
   
  public function executeList(sfWebRequest $r)
  {
    $this->prSlug = $r->getParameter('pr_slug');
    $this->projName = SER::deSlugify($this->prSlug);
    
    // Get list of search criteria from url if possible, else set the missing values to 0.
    if (isset($_GET['dueBefore']))
    {
        $dueArray = $_GET['dueBefore'];
        $this->dueBefore = $dueArray['year']."-".$dueArray['month']."-".$dueArray['day'];
    }
    else
        {$this->dueBefore="0";}
    if (isset($_GET['importance']))
        {$this->importance=$_GET['importance'];}
    else
        {$this->importance="0";}
    if (isset($_GET['group']))
        {$this->group=$_GET['group'];}
    else
        {$this->group="0";}
    if (isset($_GET['createdBy']))
        { $this->createdBy=$_GET['createdBy'];}
    else
        {$this->createdBy="0";}
    
    // Load all deadlines that don't have parents from db.
    $this->parents = Doctrine::getTable('Deadline')->getParents($this->projName);
    
  }
  
  public function executeChart(sfWebRequest $r)
  {
    $this->prSlug = $r->getParameter('pr_slug');
    $this->projName = SER::deSlugify($this->prSlug);
  }
  
  public function executeChartImage(sfWebRequest $r)
  {
  
    $this->prSlug = $r->getParameter('pr_slug');
    $this->projName = SER::deSlugify($this->prSlug);
    // GENERATING GANTT CHART.
    $g = new BURAK_Gantt();

	$g->setGrid(3);
	
	$g->setColor("group","000000");
	$g->setColor("progress","660000");
	
    $g = Doctrine::getTable('Deadline')->generateChart($this->projName, $g);
    ob_start();
    //imagejpeg($g->im);
    $g->outputGantt();
	$image = ob_get_contents();
	$length = ob_get_length();
    //imagedestroy($g->im);
	ob_end_clean();
  
    $response = $this->getResponse();
    $response->setHttpHeader('Content-type', 'image/jpeg', true);
    $response->setHttpHeader('Content-length', $length, true);
    $response->setContent($image);
        
    return sfView::HEADER_ONLY;
  }
  
  public function executeSearch(sfWebRequest $r)
  {
    $this->prSlug = $r->getParameter('pr_slug');
    $this->projName = SER::deSlugify($this->prSlug);
  
    $this->allGroups = $this->getGroupList($this->projName);
    
    $this->allUsers = array();
    foreach ($this->allGroups as $group)
    {
        $tempUsers = array();
        $tempUsers = $group->getUsers();
        foreach ($tempUsers as $user)
        {
            $this->allUsers[$user -> getID()] = $user;
        }
    }
  }
  
  public function executeEdit(sfWebRequest $r)
  {
        $this->prSlug = $r->getParameter('pr_slug');
        $projectName = SER::deSlugify($this->prSlug);
        
        if (isset($_GET['parentID']))
            $this->tempParent = $_GET['parentID'];
        else
            $this->tempParent = 0;
        
        if (isset($_GET['id']))
            $this->id = $_GET['id'];
        else
            $this->id = 0;
            
        $deadline = Doctrine::getTable('Deadline')->getDeadline($projectName, $this->id);
        
        foreach($deadline as $dead)
        {
            $this->deadline = $dead;
        }
        
        // If the id does not exist, create a new deadline.
        if (!($this->deadline) || $this->id ==0)
        {
            $this->redirect('@planning_create?pr_slug='.$this->prSlug.'&parentID='.$this->tempParent);
        }
        
        $this->form = new PlanningSearchForm();
        
        if ($r->isMethod('put'))
        {
            $this->form->bind($r->getParameter('searchForm'));
        
            if ($this->form->isValid())
            {   
                if ($this->tempParent != '0')
                    $this->deadline->setParentID($this->tempParent);
                $this->deadline->setProject(Doctrine::getTable('Project')->findOneByName($projectName));
                $dueArray = $this->form->getValue('due_date');
                $this->deadline->setDueDate($dueArray);
                $this->deadline->setNotes($this->form->getValue('notes'));
                $this->deadline->setImportance($this->form->getValue('importance'));
                $this->deadline->setTitle($this->form->getValue('title'));
                
                $this->deadline->save();
                
                $this->redirect('@planning_list?pr_slug='.$this->prSlug);
            }
        }
        
        //foreach ($this->deadline as $dead)
        //{
            $this->form->setDefaults(array('title'=>$this->deadline->getTitle(), 'importance'=>$this->deadline->getImportance(), 'notes'=>$this->deadline->getNotes(), 'due_date'=> $this->deadline->getDueDate()));
        //}
  }

  public function executeCreate(sfWebRequest $r)
  {
        $this->prSlug = $r->getParameter('pr_slug');
        $projectName = SER::deSlugify($this->prSlug);
	    if (isset($_GET['parentID']))
	        { $this->parent=$_GET['parentID'];}
	    else
	        {$this->parent=NULL;}
        
        $this->form = new PlanningSearchForm();
        
        if ($r->isMethod('put'))
        {
            $this->form->bind($r->getParameter('searchForm'));
        
            if ($this->form->isValid())
            {               
                $new = new Deadline();
                $new->setCreatedBy($this->getUser()->getID());
                if ($this->parent!=NULL)
                    $new->setParentID($this->parent);
                $new->setProject(Doctrine::getTable('Project')->findOneByName($projectName));
                $dueArray = $this->form->getValue('due_date');
                $new->setDueDate($dueArray);
                $new->setNotes($this->form->getValue('notes'));
                $new->setImportance($this->form->getValue('importance'));
                $new->setTitle($this->form->getValue('title'));
                
                $new->save();
                
                $this->redirect('@planning_list?pr_slug='.$this->prSlug);
            }
        }
        
        $this->deadline = new Deadline();
        if ($this->parent!=NULL)
            $this->deadline->setParentID($this->parent);
        $this->deadline->setProject(Doctrine::getTable('Project')->findOneByName($projectName));
  }
  
  public function executeDelete(sfWebRequest $r)
  {
        $this->prSlug = $r->getParameter('pr_slug');
        $projectName = SER::deSlugify($this->prSlug);
        if (isset($_GET['id']))
        {
	            $this->id = $_GET['id'];
	        $deadline = Doctrine::getTable('Deadline')->getDeadline($projectName, $this->id);
	        
	        foreach ($deadline as $dead)
	        {
	            $dead->delete();
	        }
        }
        
        $this->redirect('@planning_list?pr_slug='.$this->prSlug);
  }
  
  private function getDeadList($projName)
  {
     $project = Doctrine::getTable('Project')->findOneByName($projName);
     $deadlines = $project->getDeadlines();
     
     return $deadlines;
  }
  
  private function getGroupList($projectName)
  {
     $project = Doctrine::getTable('Project')->findOneByName($projectName);
     $groups = $project->getGroups();
     
     return $groups;
  }

}
?>